package com.edais.util.random;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import org.apache.log4j.Logger;


public class RandomUtils {
	private static final Logger logger = Logger.getLogger(RandomUtils.class);
	
	//随机获取0.1~1之间的一位小数
	
	public static BigDecimal getRandomDecimalByRate(){
		double max=1;
        double min=0;
        Random random = new Random();
        double randomNumber = random.nextDouble() * (max-min)+min;
        DecimalFormat digits=new DecimalFormat("0.0");
        String randomStr = digits.format(randomNumber);
        BigDecimal randomDecimal = new BigDecimal(randomStr);
        if(randomDecimal.compareTo(BigDecimal.ZERO)==0){
        	randomDecimal = new BigDecimal("0.1");
        }
        return randomDecimal;
	}
	
	//抽奖
	public static BigDecimal randomLuckDraw(){
		List<Coupons> couponsList = new ArrayList<Coupons>();
		
		//0.1
		couponsList.add(new Coupons(1,new BigDecimal("0.1"),new BigDecimal(String.valueOf(RandomEnvUtil.getValue("probability1")))));
		//0.2
		couponsList.add(new Coupons(2,new BigDecimal("0.2"),new BigDecimal(String.valueOf(RandomEnvUtil.getValue("probability2")))));
		//0.3
		couponsList.add(new Coupons(3,new BigDecimal("0.3"),new BigDecimal(String.valueOf(RandomEnvUtil.getValue("probability3")))));
		//0.4
		couponsList.add(new Coupons(4,new BigDecimal("0.4"),new BigDecimal(String.valueOf(RandomEnvUtil.getValue("probability4")))));
		//0.5
		couponsList.add(new Coupons(5,new BigDecimal("0.5"),new BigDecimal(String.valueOf(RandomEnvUtil.getValue("probability5")))));
		//0.6
		couponsList.add(new Coupons(6,new BigDecimal("0.6"),new BigDecimal(String.valueOf(RandomEnvUtil.getValue("probability6")))));
		//0.7
		couponsList.add(new Coupons(7,new BigDecimal("0.7"),new BigDecimal(String.valueOf(RandomEnvUtil.getValue("probability7")))));
		//0.8
		couponsList.add(new Coupons(8,new BigDecimal("0.8"),new BigDecimal(String.valueOf(RandomEnvUtil.getValue("probability8")))));
		//0.9
		couponsList.add(new Coupons(9,new BigDecimal("0.9"),new BigDecimal(String.valueOf(RandomEnvUtil.getValue("probability9")))));
		//1.0
		couponsList.add(new Coupons(10,new BigDecimal("1.0"),new BigDecimal(String.valueOf(RandomEnvUtil.getValue("probability10")))));
		
		List<BigDecimal> orignalRates = new ArrayList<BigDecimal>(couponsList.size());
        for (Coupons coupons : couponsList) {
        	BigDecimal probability = coupons.getProbability();
            if (probability.compareTo(BigDecimal.ZERO)<0) {
                probability = BigDecimal.ZERO;
            }
            orignalRates.add(probability);
        }
        int orignalIndex = LotteryUtil.lottery(orignalRates);
        Coupons randomCoupons = couponsList.get(orignalIndex);
		return randomCoupons.getRateRises();
	}
}
